package com.umeox.watch.moto.launcher.ui.widget;

import static com.umeox.moto.common.utils.NetworkUtil.NetworkType.MOBILE_2G;
import static com.umeox.moto.common.utils.NetworkUtil.NetworkType.MOBILE_3G;
import static com.umeox.moto.common.utils.NetworkUtil.NetworkType.MOBILE_4G;
import static com.umeox.moto.common.utils.NetworkUtil.NetworkType.MOBILE_UNKNOWN;
import static com.umeox.moto.common.utils.NetworkUtil.NetworkType.WIFI;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.BatteryManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.umeox.moto.common.utils.NetworkUtil;
import com.umeox.watch.moto.launcher.R;
import com.umeox.watch.moto.launcher.event.DeviceStateChangedListener;
import com.umeox.watch.moto.launcher.base.DeviceStatusManager;


public class BatteryAndSignalView extends RelativeLayout implements DeviceStateChangedListener {
    public static final String ACTION_SIM_STATE_CHANGED = "android.intent.action.SIM_STATE_CHANGED";
    public static final int BATTERY_STATUS_CHARGING_FULL = 9999;
    public static final int BATTERY_STATUS_DEFAULT = 0;
    private ImageView mSignalIv;
    private ImageView mBatteryIv;
    private ImageView mNetworkTypeIv;
    private Context mContext;
    private AnimationDrawable mAnimationDrawable;
    private int mBatteryAnimId;
    private int mBatteryLevelId;

    private int[] batteryAnim = {
            R.drawable.home_battery_anim_0,
            R.drawable.home_battery_anim_1,
            R.drawable.home_battery_anim_2,
            R.drawable.home_battery_anim_3

    };
    private int[] batteryLevel = {
            R.drawable.home_battery_level_0,
            R.drawable.home_battery_level_1,
            R.drawable.home_battery_level_2,
            R.drawable.home_battery_level_3

    };
    private int[] signalLevel = {
            R.drawable.home_signal_level_0,
            R.drawable.home_signal_level_1,
            R.drawable.home_signal_level_2,
            R.drawable.home_signal_level_3
    };

    private int[] wifiLevel = {
            R.drawable.home_wifi_level_0,
            R.drawable.home_wifi_level_1,
            R.drawable.home_wifi_level_2,
            R.drawable.home_wifi_level_3
    };
    private int[] cellularLevel = {
            R.drawable.home_cellular_level_0,
            R.drawable.home_cellular_level_1,
            R.drawable.home_cellular_level_2,
            R.drawable.home_cellular_level_3
    };
    private int mWallpaperIndex;
    private int mBatteryStatus;
    private int mBatteryLevel;
    private int mSignalState;

    public BatteryAndSignalView(Context context) {
        this(context, null);
    }

    public BatteryAndSignalView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BatteryAndSignalView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initView();
    }

    private void initView() {
        final View view = LayoutInflater.from(getContext()).inflate(R.layout.view_battery_signal, this);
        mBatteryIv = view.findViewById(R.id.iv_battery);
        mSignalIv = view.findViewById(R.id.iv_signal_level);
        mNetworkTypeIv = view.findViewById(R.id.iv_network_type);
        //TODO
        mWallpaperIndex = 0;//DataProvider.getWallpaper();

    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        DeviceStatusManager.getInstance().addDeviceStateChangedListener(this);
    }


    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        DeviceStatusManager.getInstance().removeDeviceStateChangedListener(this);
    }

    /**
     * 设置电池电量
     *
     * @param level
     */
    private void setupBatteryLevel(int level) {
        releaseAnimationDrawable();
        mBatteryIv.setImageResource(mBatteryLevelId);
        mBatteryIv.setImageLevel(level);
    }

    /**
     * 释放电池充电动画
     */
    private void releaseAnimationDrawable() {
        if (mAnimationDrawable != null) {
            mAnimationDrawable.stop();
            mBatteryIv.setImageDrawable(null);
        }
    }

    private static final String TAG = "zhanlv";

    private void initActiveNetworkType(String networkType) {
        Log.d(TAG, "initActiveNetworkType: " + networkType);
        mNetworkTypeIv.setVisibility(VISIBLE);
        if (WIFI.equals(networkType)) {
            mNetworkTypeIv.setImageResource(wifiLevel[mWallpaperIndex]);
            return;
        }
        mNetworkTypeIv.setImageResource(cellularLevel[mWallpaperIndex]);
        switch (networkType) {
            case MOBILE_2G:
                mNetworkTypeIv.setImageLevel(0);
                break;
            case MOBILE_3G:
                mNetworkTypeIv.setImageLevel(1);
                break;
            case MOBILE_4G:
                mNetworkTypeIv.setImageLevel(2);
                break;
            case MOBILE_UNKNOWN:
            default:
                mNetworkTypeIv.setVisibility(GONE);
                break;
        }
    }


    private void setupBattery() {

        switch (mBatteryStatus) {
            case BatteryManager.BATTERY_STATUS_CHARGING:
                startBatteryAnimation();
                break;
            case BatteryManager.BATTERY_STATUS_DISCHARGING:
                setupBatteryLevel(mBatteryLevel);
                break;
            case BatteryManager.BATTERY_STATUS_FULL:
                setupBatteryLevel(BATTERY_STATUS_CHARGING_FULL);
                break;

        }
    }

    private void startBatteryAnimation() {
        mBatteryIv.setImageResource(mBatteryAnimId);
        mAnimationDrawable = (AnimationDrawable) mBatteryIv.getDrawable();
        mAnimationDrawable.start();
    }

    public void onWallpaperChange(int position) {
        mWallpaperIndex = position;
        mBatteryAnimId = batteryAnim[position];
        mBatteryLevelId = batteryLevel[position];
        int signalLevelId = signalLevel[position];
        mSignalIv.setImageResource(signalLevelId);
        onSignalStatusChange(mSignalState);
        switch (mBatteryStatus) {
            case BATTERY_STATUS_DEFAULT:
            case BatteryManager.BATTERY_STATUS_FULL:
                setupBatteryLevel(BATTERY_STATUS_CHARGING_FULL);
                break;
            case BatteryManager.BATTERY_STATUS_CHARGING:
                releaseAnimationDrawable();
                startBatteryAnimation();
                break;
            case BatteryManager.BATTERY_STATUS_DISCHARGING:
                setupBatteryLevel(mBatteryLevel);
                break;
        }
    }

    @Override
    public void onSignalStatusChange(int state) {
        mSignalState = state;
        if (state == -1) {
            mSignalIv.setVisibility(View.GONE);
            if (!NetworkUtil.isNetworkTypeWifi(mContext)) {
                mNetworkTypeIv.setVisibility(View.GONE);
            }
            return;
        }
        mSignalIv.setVisibility(View.VISIBLE);
        mSignalIv.setImageLevel(state);
    }

    @Override
    public void onBatteryStatusChange(int batteryStatus, int batteryLevel) {
        mBatteryStatus = batteryStatus;
        mBatteryLevel = batteryLevel;
        setupBattery();
    }

    @Override
    public void onNetworkTypeChange(String networkType) {
        initActiveNetworkType(networkType);
    }

    @Override
    public void onWifiSignalLevelChange(int wifiSignalLevel) {
        mNetworkTypeIv.setImageLevel(wifiSignalLevel);
    }
}
